import { readonly, ref } from 'vue'
import { defineStore } from 'pinia'

export const useVideoTimeBarStore = defineStore('videoTimeBar', () => {
  const currentPlayTime = ref(0)
  const currentPlayRate = ref(0)
  const rate = ref(null)
  const seeking = ref(false)

  function updateTime(v) {
    currentPlayTime.value = v
  }
  function start() {
    seeking.value = true
  }


  function stop() {
    seeking.value = false
  }
  function updateRate(v) {
    currentPlayRate.value = Math.max(0, Math.min(1, v))
  }
  function seek(v) {
    rate.value = Math.max(0, Math.min(1, v))
  }
  function clear() {
    rate.value = null
  }
  function reset() {
    currentPlayRate.value = 0
    rate.value = null
    currentPlayTime.value = 0
    seeking.value = false
  }

  return {
    rate: readonly(rate),
    currentPlayTime: readonly(currentPlayTime),
    currentPlayRate: readonly(currentPlayRate),
    seeking: readonly(seeking),
    start,
    stop,
    seek,
    clear,
    updateTime,
    updateRate,
    reset
  }
})
